package com.whnk.pm.service.impl;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.whnk.common.config.Global;
import com.whnk.common.constant.ConfigConstants;
import com.whnk.common.utils.file.FileUtils;
import com.whnk.pm.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.pm.mapper.FileInfoMapper;
import com.whnk.pm.domain.FileInfo;
import com.whnk.pm.service.IFileInfoService;
import com.whnk.common.support.Convert;

/**
 * 人员附件 服务层实现
 * 
 * @author whwhnk
 * @date 2019-06-13
 */
@Service
public class FileInfoServiceImpl implements IFileInfoService 
{
	@Autowired
	private FileInfoMapper fileInfoMapper;

	@Autowired
	private ConfigConstants configConstants;
	/**
     * 查询人员附件信息
     * 
     * @param fileId 人员附件ID
     * @return 人员附件信息
     */
    @Override
	public FileInfo selectFileInfoById(Integer fileId)
	{
		FileInfo fileInfo = fileInfoMapper.selectFileInfoById(fileId);
		try {
			String filePath = configConstants.getFileBaseDir() + fileInfo.getEmpFileId();
			File fileResource = new File(filePath);
			byte[] bytes = FileUtils.File2byte(fileResource);
			File file = FileUtil.downloadFile(fileInfo.getEmpAttType(), fileInfo.getEmpFileNm(), bytes);
			fileInfo.setEmpFileNm(file.getName());
		} catch (IOException e) {
			throw new RuntimeException();
		}
		return fileInfo;
	}
	
	/**
     * 查询人员附件列表
     * 
     * @param fileInfo 人员附件信息
     * @return 人员附件集合
     */
	@Override
	public List<FileInfo> selectFileInfoList(FileInfo fileInfo)
	{
	    return fileInfoMapper.selectFileInfoList(fileInfo);
	}
	
    /**
     * 新增人员附件
     * 
     * @param fileInfo 人员附件信息
     * @return 结果
     */
	@Override
	public int insertFileInfo(FileInfo fileInfo)
	{
		byte[] imgbytes = fileInfo.getEmpAtt();
		if (imgbytes != null && imgbytes.length >= 1) {
			String uuid = UUID.randomUUID().toString();
			String filePath = configConstants.getFileBaseDir() + uuid;
			com.whnk.common.utils.file.FileUtils.saveFile(imgbytes, filePath);
			fileInfo.setEmpFileId(uuid);
			fileInfo.setEmpAtt(null);
		}
	    return fileInfoMapper.insertFileInfo(fileInfo);
	}
	
	/**
     * 修改人员附件
     * 
     * @param fileInfo 人员附件信息
     * @return 结果
     */
	@Override
	public int updateFileInfo(FileInfo fileInfo)
	{
		byte[] imgbytes = fileInfo.getEmpAtt();
		if (imgbytes != null && imgbytes.length >= 1) {
			String uuid = UUID.randomUUID().toString();
			String filePath = configConstants.getFileBaseDir() + uuid;
			com.whnk.common.utils.file.FileUtils.saveFile(imgbytes, filePath);
			fileInfo.setEmpFileId(uuid);
			fileInfo.setEmpAtt(null);
		}
	    return fileInfoMapper.updateFileInfo(fileInfo);
	}

	/**
     * 删除人员附件对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFileInfoByIds(String ids)
	{
		return fileInfoMapper.deleteFileInfoByIds(Convert.toStrArray(ids));
	}

	/**
	 * 根据人员证件号码，查询人员附件列表
	 * 
	 * @param empId
	 * @return
	 */
	@Override
	public String selectFilesByEmpId(String empId) {
		List<FileInfo> fileInfoList = fileInfoMapper.selectFilesForEmpId(empId);
		List<File> fileList = new ArrayList<>();
		String empName = "";
		for (FileInfo fileInfo : fileInfoList) {
			try {
				empName = fileInfo.getEmpNm();
				String filePath = configConstants.getFileBaseDir() + fileInfo.getEmpFileId();
				File fileResource = new File(filePath);
				byte[] bytes = FileUtils.File2byte(fileResource);
				File file = FileUtil.downloadFile(fileInfo.getEmpAttType(), fileInfo.getEmpFileNm(), bytes);
				fileList.add(file);
			} catch (IOException e) {
				throw new RuntimeException();
			}
		}
		File zipFileName = new File(Global.getDownloadPath() + empName + "-" + empId + ".zip");
		try {
			FileUtil.zipFile(zipFileName, fileList);
		} catch (IOException e) {
			throw new RuntimeException();
		}
		return zipFileName.getName();
	}
	
}
